package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 根据查询条件分页并排序查询品牌信息
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    @Override
    public PageResult<Brand> queryBrandsByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {

        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        //根据name模糊查询，或者根据首字母查询
        if(StringUtils.isNotBlank(key)){
            criteria.andLike("name", "%" + key + "%").orEqualTo("letter", key);
        }
        //添加分页条件
        PageHelper.startPage(page, rows);
        //添加排序条件
        if(StringUtils.isNotBlank(sortBy) && desc != null){
            example.setOrderByClause(sortBy + " " + (desc ? "desc" : "asc"));
        }
        List<Brand> brands = brandMapper.selectByExample(example);
        //包装成pageInfo
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        //包装分页结果返回
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增品牌
     * @param brand
     */
    @Transactional
    @Override
    public void saveBrand(Brand brand, List<Long> cids) {

        //先新增brand
        int result = brandMapper.insertSelective(brand);
        if(result == 1){
            //新增中间表
            cids.forEach(cid -> {
                brandMapper.insertCategoryAndBrand(cid, brand.getId());
            });
        }
    }

    /**
     * 对品牌进行修改
     * @param brand
     * @param cids
     * @return
     */
    @Transactional
    @Override
    public void updateBrand(Brand brand, List<Long> cids) {

       brandMapper.updateByPrimaryKey(brand);
       //删除中间表
       brandMapper.deleteCidByBid(brand.getId());
        for (Long cid : cids) {
            brandMapper.insertCategoryAndBrand(brand.getId(), cid);
        }
    }

    /**
     * 对品牌进行删除
     * @param bid
     * @return
     */
    @Transactional
    @Override
    public void deleteBrandByBid(Long bid) {
        Brand brand = new Brand();
        brand.setId(bid);
        brandMapper.delete(brand);
        brandMapper.deleteCidByBid(bid);
    }

    /**
     * 根据分类id查询品牌列表
     * @param cid
     * @return
     */
    @Override
    public List<Brand> queryBrandsByCid(Long cid) {
        return brandMapper.selectBrandsByCid(cid);
    }

    @Override
    public Brand queryBrandsByid(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }
}